<?php
/*
商品详情
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;

$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return=array();
$basic=$this->setting;
$return['set']=$basic;
$return['setting']=$basic;
if($_RQ['op']=='main'){
	$id=$_RQ['id'];
	if(!$id){
		exi('商品已下架或被删除!','error');
	}
	$_SESSION['goodsid'] = $id;
	$goods=pdo_get('jy_creditshop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	//增加一次浏览次数
	pdo_update('jy_creditshop_goods', array('views' => $goods['views'] + 1), array('id' => $id));
	if(is_array($goods['dispatch'])){
		$isarray_dispatch=1;
	}else{
		$isarray_dispatch=0;
	}
	$res=model('jy_creditshop/order')->check_buy($goods);
	$goods['canbuy']=$res['canbuy'];
	$goods['buymsg']=$res['buymsg'];
	//规格
	$specs_list=pdo_getall('jy_creditshop_spec',array('goodsid'=>$id,'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','displayorder asc');
	//已选规格
	$selectspecs = array();
	$selespename = '';
	if($specs_list && !$goods['goodstype']){
			foreach($specs_list as $k=>&$sp){
				$sp['items']=pdo_getall('jy_creditshop_spec_item',array('specid'=>$sp['id'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','displayorder asc');
				$selectspecs[$k]=$sp['items'][0]['id'];
				$selespename .=$sp['items'][0]['title'].'，';
			}
	}
	//默认已选参数：
	$spsids = '';
	foreach($selectspecs as $k=>&$s){
			if($k==0){
				$spsids .= $s;
			}else{
				$spsids .= '_'.$s;
			}
	}
	$return['options'] = pdo_get('jy_creditshop_option',array('goodsid'=>$id,'specs'=>$spsids));
	//默认商品规格价格：
	if($return['options']){
		$goods['money']=$return['options']['price'];
		$goods['credit']=$return['options']['credit'];
	}
	$return['selespename'] = $selespename;
	$option_list=pdo_getall('jy_creditshop_option',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$id),array('id','specs','thumb','total','price','credit','goodssn','productsn','weight'),'','displayorder asc');
	if($option_list){
			foreach($option_list as &$i){
				$i['specs']=explode('_',$i['specs']);
				foreach($i['specs'] as $j){
					$op=pdo_get('jy_creditshop_spec_item',array('id'=>intval($j)),array('specid','title'));
					$spec_title=pdo_getcolumn('jy_creditshop_spec',array('id'=>$op['specid']),'title');
					$i[$spec_title]=$op['title'];
				}
				$i['total']=$i['total'];
				$i['credit']=$i['credit'];
				$i['goodssn']=$i['goodssn'];
				$i['productsn']=$i['productsn'];
				$i['thumb']=$i['thumb'];
			}
			$specs_list['option']=$option_list;
	}
	$return['specs_list'] = $specs_list;
	//评价
	$params=array(
			'table'=>'jy_creditshop_comment',
			'params'=>array('goodsid'=>$id,'uniacid'=>$_SESSION['uniacid']),
			'fields'=>'',
			'key'=>'',
			'orderby'=>'time desc',
			'limit'=>'3',
	);
	$pindex = max(1, intval($_RQ['page']));
	$psize = 30;
	$total='';
	$comments = pdo_getslice($params['table'],$params['params'],array($pindex,$psize),$total,$params['fields']);
	//参与记录
	$params=array(
			'table'=>'jy_creditshop_log',
			'params'=>array('goodsid'=>$id,'uniacid'=>$_SESSION['uniacid']),
			'fields'=>'',
			'key'=>'',
			'orderby'=>'createtime desc',
			'limit'=>'',
	);
	$log = pdo_getslice($params['table'],$params['params'],array($pindex,$psize),$total,$params['fields']);
	$logtotal=0;
	if($log){
		//参与记录次数
		$logtotal=pdo_count('jy_creditshop_log',$params['params'],'id');
		foreach($log as &$item){
			$member=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['mid']));
			$item['avatar'] = $member['avatar'];
			$item['nickname'] = $member['nickname'];
			$item['createtime_str'] = date('Y/m/d H:i', $item['createtime']);
		}
	}
	//推荐商品
	$params=array(
			'table'=>'jy_creditshop_goods',
			'params'=>array('credit_type'=>$goods['credit_type'],'status'=>1,'uniacid'=>$_SESSION['uniacid'],'isrecommand'=>'1','merchid'=>$_SESSION['onemerchid']),
			'fields'=>'',
			'key'=>'',
			'orderby'=>'displayorder asc',
			'limit'=>'3',
	);
	$total=0;
	$pindex = 1;
	$psize = 10;
	$goodsrec=pdo_getslice('jy_creditshop_goods',$params['params'],array($pindex,$psize),$total,$params['fields'],'','');
	$goodsrec=array($goodsrec['0'],$goodsrec['1'],$goodsrec['2']);
	//POST提交：
	$post = array();
	$post['num'] = 1;
	$post['goodsid'] = $id;
	$post['specid']=$selectspecs;
	$post['optionid']=$return['options']['id'];
	//$post['addressid']=$address['id'];
	$return['comment'] = $comment;
	$return['log'] = $log;
	$return['logtotal'] = $logtotal;
	$return['goodsrec'] = $goodsrec;
	if($goods['images']){
		$goods['images']=json_decode($goods['images'],true);
	}else{
		$goods['images'][]=$goods['thumb'];
	}
	$goods['creditmore']=json_decode($goods['creditmore'],true);
	$credit_types=credit_types(true);
	foreach($goods['creditmore'] as &$gv){
		$gv['credit_title']=$credit_types[$gv['credit_type']];
	}
	if($goods['is_moretype']){
		$post['taocan']=$goods['creditmore'][0];
	}else{
		$post['taocan']=array(
			'credit_title'=>$credit_types[$goods['credit_type']],
			'credit'=>$goods['credit'],
			'money'=>$goods['money'],
		);
	}
	$return['goods'] = $goods;
	$return['post']=$post;
	set_html('title',$goods['title']?:'商品详情');
	$return['sharedata']=array(
		'title'=>$goods['title']?:'商品详情',
		'desc'=>$goods['title']?$goods['title'].',特价兑换！':'特价兑换！',
		'link'=>custom_url('jy_creditshop/goods_detail//id='.$id,true),
		'imgUrl'=>tomedia($goods['thumb']),
	);
	exi($return);
}
if($_RQ['op']=='exchange'){
	$post=get_postdata();
	$id=intval($post['goodsid']);
	$number=intval($post['num']);
	if(!$id){
		exi('商品不存在或已删除！','error');
	}
	if(!$number){
		$number=1;
	}
	$spsids = '';
	foreach($post['specid'] as $k=>&$s){
			if($k==0){
				$spsids .= $s;
			}else{
				$spsids .= '_'.$s;
			}
	}
}
if($_RQ['op']=='get_optionsid'){
	$post=get_postdata();
	$id=$_SESSION['goodsid'];
	if(!$id){
		exi('请先选择商品！','error');
	}
	
	//规格
	$specids=explode('_',$post['specid']);
	
	//已选规格
	$selespename = '';
	if($specids){
			foreach($specids as $k=>$sp){
				$spec_item=pdo_get('jy_creditshop_spec_item',array('id'=>$sp,'uniacid'=>$_SESSION['uniacid']),array('id','title'));
				$selespename .=$spec_item['title'].'，';
			}
	}
	$return['options'] = pdo_get('jy_creditshop_option',array('goodsid'=>$id,'specs'=>$post['specid']));
	//默认商品规格价格：
	if($return['options']){
		$goods['price']=$return['options']['price'];
	}
	$return['selespename'] = $selespename;
	exi($return);
}